package by.wind.drebedengi.asynctask;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;

public abstract class AbstractTask extends AsyncTask<Long, Integer, Boolean>{
	
	protected Context itsContext;
	protected Boolean itsResult;
	protected IProgressTracker itsProgressTracker;
	protected String itsProgressMessage;
	
	public abstract void setProgressTracker(IProgressTracker progressTracker);
	
	protected abstract int getProgressType();
	
	abstract public String onErrorMessage();
	
	AbstractTask(Context aContext) {
		itsContext = aContext;
	}
	
	/* UI Thread */
    @Override
    protected void onCancelled() {
	// Detach from progress tracker
    	itsProgressTracker = null;
    }
    
    @Override
    protected void onPostExecute(Boolean result)
    {
		// Update result
		itsResult = result;
		// And send it to progress tracker
		if (itsProgressTracker != null) {
		    itsProgressTracker.onComplete();
		}
		// Detach from progress tracker
		itsProgressTracker = null;
    }
    
    public boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) itsContext.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
}
